<template>
  <div>
    <!-- 操作区 -->
    <div class="gongneng">
      <a-checkbox @change="dataSelectChange" v-model="qx" class="font"
        >全选/取消
      </a-checkbox>
      <a-button type="primary" size="small" ghost @click="counterelection"
        >反选</a-button
      >&nbsp;
      <a-button type="primary" size="small" class="shangb" @click="TopB"
        >上报</a-button
      >&nbsp;
      <a-button type="danger" size="small" @click="withdraw">撤回</a-button
      >&nbsp;
      <a-button type="primary" size="small" icon="minus" @click="deleteAll"
        >删除</a-button
      >&nbsp;
      <a-button type="primary" size="small" icon="plus" @click="dataAdd"
        >新增</a-button
      >
    </div>
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 19 }"
        @keyup.enter.native="searchQuery"
      >
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目名称:" class="font">
              <a-input v-model="queryParam.pjtname"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="行政区:" class="font">
              <a-select
                v-model="queryParam.city"
                @change="changeCity"
                :disabled="isInput"
              >
                <a-select-option
                  :value="item.no"
                  v-for="(item, index) in citydata"
                  :key="index"
                  >{{ item.name }}</a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="7" :md="8" :sm="24">
            <a-form-item>
              <a-select v-model="queryParam.area" :disabled="isInput">
                <a-select-option
                  :value="item.no"
                  v-for="(item, index) in xiandata"
                  :key="index"
                  >{{ item.name }}</a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span
              style="float: left; overflow: hidden"
              class="table-page-search-submitButtons"
            >
              <a-button type="primary" @click="searchQuery" icon="search"
                >查询</a-button
              >
              <a-button
                type="primary"
                @click="searchReset"
                icon="reload"
                class="cz"
                >重置</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      bordered
      rowKey="dataSec"
      ref="table"
      :scroll="{ x: 0 }"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      @change="handleTableChange"
      :row-selection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
      }"
    >
      <span slot="pjtcount" slot-scope="text, record">
        <a @click="detaildata(text, record)">{{ text }}</a>
      </span>
      <span slot="xmName" slot-scope="text, record">
        <a @click="detaildata(text, record)">{{ text }}</a>
      </span>
      <span slot="action" slot-scope="text, record">
        <a v-if="record.xmZt == 1" @click="cacelsb(record)">撤回上报</a>
        <span v-else>
          <a @click="update(record)">修改</a>&nbsp; <a @click="deletedata(record)">删除</a>&nbsp;
          <a @click="Shangnba(record)">上报</a></span
        >
      </span>
    </a-table>
  </div>
</template>

<script>
import {
  ssnydLandlist,
  cancel,
  ssnydlandsubmit,
  ssnydlandel,
  uuid,
} from "../../api/api";
import {tableminx} from '../../mixins/tablemix'
export default {
  mixins:[tableminx],
  data() {
    return {
      columns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 50,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: "项目编号",
          dataIndex: "xmXmbh",
          width: 150,
          align: "left",
          scopedSlots: { customRender: "pjtcount" },
        },
        {
          title: "项目名称",
          dataIndex: "xmName",
          align: "left",
          scopedSlots: { customRender: "xmName" },
          width: 200,
        },
        {
          title: "行政区",
          dataIndex: "xmXzqmc",
          align: "left",
          width: 150,
        },
        {
          title: "备案编号",
          dataIndex: "xmBabh",
          align: "left",
          width: 110,
        },
        {
          title: "设施农业类型",
          dataIndex: "xmSsnylx",
          width: 200,
          align: "left",
          customRender: (xmSsnylx, record, index) => {
            var ssnylxs = record.xmSsnylx;
            console.log(ssnylxs, "1111");
            var ssnylstr = "";
            if (ssnylxs == null || ssnylxs == "") {
              return "";
            } else {
              var ssnyssz = ssnylxs.split(",");
              if (ssnyssz.length > 0) {
                for (var i = 0; i < ssnyssz.length; i++) {
                  if (ssnyssz[i].length > 0) {
                    if (ssnyssz[i] == "1") {
                      ssnylstr += "作物种植设施农用地  ";
                    } else if (ssnyssz[i] == "2") {
                      ssnylstr += "畜禽水产养殖设施农用地  ";
                    }
                  }
                }
              }
              return ssnylstr;
            }
          },
        },
        {
          title: "设施农业申请用地面积(公顷)",
          dataIndex: "xmZydmj",
          align: "left",
          width: 200,
        },
        {
          title: "生产设施用地面积(公顷)",
          dataIndex: "xmScssydmj",
          align: "left",
          width: 170,
        },
        {
          title: "辅助设施用地(公顷)",
          dataIndex: "xmFzssydmj",
          width: 140,
          align: "left",
        },
        {
          title: "操作",
          width: 120,
          dataIndex: "action",
          align: "center",
          scopedSlots: { customRender: "action" },
        },
      ],
      selectedRowKeys: [],
      dataSource: [],
      iscont: false,
      qx: false,
    };
  },
  mounted() {
    this.dataList();
    this.getCtiydata();
    this.changeCity();
  },
  methods: {
    //获取报件箱列表数据
    async dataList() {
      if (this.user.role == "xian") {
        this.queryParam.area = this.user.fkDept;
        this.queryParam.city = this.user.fkDept.slice(0, 4);
        this.isInput = true;
      }
      const pag = this.ipagination;
      const params = {
        keyWord: this.queryParam.pjtname,
        pageNo: pag.current,
        pageSize: pag.pageSize,
        city: this.queryParam.city,
        area: this.queryParam.area,
      };
      const data = await ssnydLandlist(params);
      if (data.code == 200) {
        this.dataSource = data.result.records;
        pag.total = data.result.total;
      }
    },
    //表格复选框事件
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys;
    },
    handleTableChange(pagination, filters, sorter) {
      this.ipagination.current = pagination.current;
      this.ipagination.pageSize = pagination.pageSize;
      this.dataList();
      this.selectedRowKeys = [];
      this.qx = false;
    },
    //全选/取消事件
    dataSelectChange(e) {
      var arr = [];
      this.dataSource.map((item, index) => {
        arr.push(item.dataSec);
      });
      console.log(arr, "arr");
      this.selectedRowKeys = e.target.checked == true ? arr : [];
    },
    //查询
    searchQuery() {
      this.dataList();
    },
    //重置
    searchReset() {
      this.queryParam = { area: "", city: "" };
      this.dataList();
    },
    //反选
    counterelection() {
      if (this.qx == true || this.selectedRowKeys.length > 0) {
        this.qx = false;
        this.selectedRowKeys = [];
      } else {
        this.iscont = !this.iscont;
        const arr = [];
        this.dataSource.map((item, index) => {
          arr.push(item.dataSec);
        });
        this.selectedRowKeys = this.iscont == true ? arr : [];
      }
    },
    //跳转到设施农业用地详情
    detaildata(text, data) {
      this.$router.push({
        path: "/admin/agriculand",
        query: {
          type: "detail",
          id: data.dataSec,
        },
      });
    },
    //上报
    async TopB() {
      if (this.selectedRowKeys.length > 0) {
        const params = {
          ids: this.selectedRowKeys.join(","),
          status: 1,
        };
        const data = await ssnydlandsubmit(params);
        if (data.code == 500) {
          this.$warning({
            title:'数据检查',
            width:500,
            content: h => {
              return h('div', { domProps: { innerHTML:data.message } })
            },

            okText: "确定",
          });
          this.dataList();
          this.selectedRowKeys = []
        }else
        {
          this.$success({
            content: data.message
          })
          this.dataList();
        }
      } else {
        this.$warning({
          content: "请选择操作的数据",
          okText: "确定",
        });
      }
    },
    //撤回
    async withdraw() {
      if (this.selectedRowKeys.length > 0) {
        const data = await cancel({ ids: this.selectedRowKeys.join(","),status:1 });
        if (data.code == 200) {
          this.$success({
            content: '撤回成功',
            okText: "确定",
          });
          this.dataList();
          this.selectedRowKeys = []
        }
      } else {
        this.$warning({
          content: "请选择操作的数据",
          okText: "确定",
        });
      }
    },
   async cacelsb(res){
        const data = await cancel({ ids: res.dataSec,status:1 });
        if (data.code == 200) {
          this.$success({
            content: '撤回成功',
            okText: "确定",
          });
          this.selectedRowKeys = []
          this.dataList();
        }
    },
    //删除
   async deleteAll() {
      if(this.selectedRowKeys.length > 0)
      {
        let that = this
        this.$confirm({
          title: '确定要删除吗?',
           cancelText: '取消',
           okText: "确定",
          onOk:() =>{
            return new Promise(async(resolve, reject) => {
              const data = await ssnydlandel({ dataSec:that.selectedRowKeys.join(","),isqz:''  })
              if(data.code == 200)
              {
                this.$message.success(data.message)
                this.dataList();
                this.selectedRowKeys = []
                resolve() 
              }else
              {
                this.$message.error(data.message)
                reject() 
              }
             
              }).catch()
          }
        })
        
      }else
      {
        this.$warning({
        content: "请选择操作的数据",
        okText: "确定",
      });
      }

    },
    //新增
   async dataAdd() {
        const data = await uuid()
      this.$router.push({
        path: "/admin/agriculand",
        query: {
          type: "add",
          id:data.result
        },
      });
    },
   async Shangnba(res){
        const data = await ssnydlandsubmit({ ids: res.dataSec, status: 1, });
        if (data.code == 500) {
          this.$warning({
            title:'数据检查',
            width:500,
            content: h => {
              return h('div', { domProps: { innerHTML: data.message} })
            },

            okText: "确定",
          });
          this.dataList();
        }else
        {
          this.$success({
            content: data.message,
            okText: "确定",
          });
          this.dataList();
        }
      
    },
    update(data) {
      this.$router.push({
        path: "/admin/agriculand",
        query: {
          type: "detail",
          id:data.dataSec
        },
      });
    },
    //删除
    deletedata(data){
      var res= data
      let that = this
        this.$confirm({
          title: '确定要删除吗?',
           cancelText: '取消',
           okText: "确定",
          onOk:() =>{
            return new Promise(async(resolve, reject) => {
              const data = await ssnydlandel({ dataSec:res.dataSec,isqz:'' })
              if(data.code == 200)
              {
                this.$message.success(data.message)
                this.dataList();
                resolve() 
              }else
              {
                this.$message.error(data.message)
                resolve() 
              }
             
              }).catch()
          }
        })
    }
  },
};
</script>

<style lang="less" scoped>
/deep/ .ant-table-align-center {
  //   background: #e4f6ff !important;
  font-weight: bold !important;
}
/deep/ .ant-table-align-left {
  font-weight: bold !important;
}
.font {
  font-weight: 700;
  font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", sans-serif;
  color: #4d5259;
}
.gongneng {
  padding-bottom: 15px;
  .shangb {
    margin-left: 50px;
  }
  border-radius: 4px;
  border: 1px solid #f0f0f0;
  padding: 10px;
}
.cz {
  margin-left: 8px;
}
.table-page-search-wrapper {
  margin-top: 10px;
}
/deep/ .ant-table-body {
  //样式穿透
  overflow-x: auto !important;
}
</style>